//
//  OnePlatform.m
//  reSearchDemo
//
//  Created by 云库 on 15/7/24.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "OnePlatform.h"
#import "YunPlatform.h"
#import "HTTPGlobals.h"
#import "OnePlatformCell.h"
#import "OnePlatformModel.h"
#import "PlatformFunction.h"
#import "PlatformManagement.h"
#import "PlatformMerchantsVC.h"
#import "PlatformMy.h"
#import "RegisterViewController.h"
#import "YunkuPlatformHome.h"

@interface OnePlatform () {
    NSMutableArray *dataSource;    /**< 数据源  */
    UITableView * mTableView;
}

@end

@implementation OnePlatform

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.title = @"我的云平台";
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menuAction)];
    
    UITableView * table=[[UITableView alloc]initWithFrame:CGRectMake(0,10, self.view.frame.size.width, self.view.frame.size.height)];
    table.delegate=self;
    table.dataSource=self;
    table.rowHeight=40;
//    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.tableFooterView = [[UIView alloc] init];
    mTableView = table;
    [self.view addSubview:table];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
    back.title = @"";
    self.navigationItem.backBarButtonItem = back;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self startRequest];
}

-(void)menuAction {
    [self pushViewController:[RegisterViewController new]];
}

/** 开始登陆到服务器*/
- (BOOL)startRequest {
    if ([super startRequest]) {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        NSString *tfUserName = [defaults objectForKey:KBSLoginUserName];
        NSString *tfPassWord = [defaults objectForKey:KBSLoginPassWord];
        [client loginWithUserPhone:tfUserName password:tfPassWord];
    }
    return YES;
}

/***/
- (BOOL)requestDidFinish:(BSClient*)sender obj:(NSDictionary *)obj {
    if ([super requestDidFinish:sender obj:obj]) {
        id data = [obj getDictionaryForKey:@"data"];
        if (data && [data isKindOfClass:[NSDictionary class]]) {
            User * item = [User objWithJsonDic:data];
            if (item) {
                // 登录成功
                [self loadDataSource];
            } else {
                // 登录失败
                sender.errorMessage = @"无法获取登录用户信息";
                [sender showAlert];
                [self popViewController];
            }
        }
    } else {
        [self popViewController];
    }
    return YES;
}
#pragma mark -- 加载数据源 --
- (void)loadDataSource {
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/index/platformList.html" parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            if (obj[@"status"] && [obj[@"status"] isEqualToString:@"1"]) {
                [self showText:obj[@"info"]];
                return ;
            }
            
            NSInteger code = [obj[@"code"] integerValue];
            dataSource = [NSMutableArray array];
            if (code == 0) {
                NSArray *dataArray = obj[@"data"];
                for (NSDictionary *dic in dataArray) {
                    OnePlatformModel *model = [[OnePlatformModel alloc] initWithDictionary:dic];
                    [dataSource addObject:model];
                }
            }
            [mTableView reloadData];
        } else {
            [self popViewController];
        }
    }];
}

#pragma mark - UITableViewDelegate&UITableViewDataSourse
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OnePlatformCell * cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[OnePlatformCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setModel:dataSource[indexPath.row]];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.loading = YES;
    OnePlatformModel *model = dataSource[indexPath.row];
    NSDictionary *parameter = @{@"account":model.account};
    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/index/login.html" parameters:parameter finish:^(BOOL seccess, NSString *error, id obj) {
        self.loading = NO;
        if (seccess) {
            NSNumber *status = obj[@"status"];
            if (status.intValue != 1) {
                [self showText:@"平台登录失败"];
                return ;
            }
            YunkuPlatformHome *yunkuPlatformHome = [[YunkuPlatformHome alloc]initWithPlatformModel:model];
            [self pushViewController:yunkuPlatformHome];
        }
    }];
}

@end
